const app = getApp()
const dayjs = require("./dayjs");
var WxNotificationCenter = require("../../config/WxNotificationCenter");
import userapi from '../../config/api/userapi' // 引入api接口管理文件

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ntop: 0,
    nheight: 0,
    PhoneShow: false,
    Vname: "企业智能化办公",
    Logo: "../../static/images/banner1.jpeg",
    loginSuccess: false,

    userinfo: null,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {



    // wx.showLoading({
    //   title: '加载中...',
    // })
    let bar = wx.getMenuButtonBoundingClientRect() // 获取胶囊丸信息
    // // 胶囊的top有时候获取到是0 ，所以兼容 statusBarHeight（刘海的高度，原生标题栏的top，比胶囊的top高一些）
    let top = bar.top || wx.getSystemInfoSync().statusBarHeight + 4;

    this.setData({
      nheight: bar.height, // 标题栏高度与胶囊 高度相同，适用于透明标题栏。
      ntop: top // 标题栏top。如果标题栏高度比胶囊大，设置为 top - (navigationHeight - bar.height)/2
    })
    let that = this;
    WxNotificationCenter.addNotification("loginsuccess", that.loginSuccess, that);

  },
  //请求场馆信息
  getvenueData() {
    let that = this;
    if (app.globalData.Vid) {

      http.venueInfo({
        'Vid': app.globalData.Vid
      }, function (res) {
        let resdata = res.data;
        let {
          code,
          data
        } = resdata;
        if (code == 200) {
          console.log(data);
          that.setData({
            Vname: data.Vname,
          });
          if (data.Logo && data.Logo.length) {
            that.setData({
              Logo: data.Logo,
            });
          }
        }
      })
    }
  },

  //登录成功
  loginSuccess() {
    wx.hideLoading({
      success: (res) => {},
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  loginFiled() {
    console.log("登录失败");
  },
  thatnophone() {
    if (app.globalData.wxavatarUrl.length > 0) {
      //缺少手机号
      this.setData({
        'PhoneShow': true
      });
    }

    wx.hideLoading({
      success: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  clickLogin() {

    var that = this;
    wx.getUserProfile({
      desc: '将获取您的基础信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let rawData = JSON.parse(res.rawData)
        console.log(rawData);
        that.setData({
          userinfo: rawData,
          PhoneShow: true,
        })
      }
    })
  },

  getPhoneNumber(e) {
    let code = e.detail.code;

    let vm = this;
    userapi.getphone({
      "code": code,
    }, function (res) {

      let data = JSON.parse(res.data.msg);
      let phone = data.phone_info.phoneNumber;

      let openid = app.globalData.openid;
      userapi.save({
        "openId": openid,
        "name": vm.data.userinfo.nickName,
        "phone": phone
      }, function (res) {
        wx.showToast({
          title: res.data.msg,
        })
        app.globalData.userInfo = vm.data.userinfo;
        app.globalData.islogin = true;
        WxNotificationCenter.postNotificationName("clickloginsuccess");
        wx.navigateBack({
          delta: 1,
        })
      })

      // }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    WxNotificationCenter.removeNotification("loginsuccess", that);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})